﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using System.Windows;
using System.Windows.Controls;
using SHomeWorkshop.LunarMind.Dialogs;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Windows.Documents;

namespace SHomeWorkshop.LunarMind.Commands
{
    /// <summary>
    /// 打印文档到A4纸命令。此命令总是可以运行的。
    /// </summary>
    public static class PrintDocumentToA4PaperCommand
    {
        private static RoutedUICommand routedUICmd;//创建私有字段

        public static RoutedUICommand RoutedUICmd//创建只读静态属性
        {
            get { return routedUICmd; }
        }

        static PrintDocumentToA4PaperCommand()//类型构造器
        {
            routedUICmd = new RoutedUICommand(
                "PrintDocumentToA4PaperCommand",
                "PrintDocumentToA4PaperCommand",
                typeof(PrintDocumentToA4PaperCommand),//创建RoutedUICommand对象
                new InputGestureCollection() 
                { 
                    new KeyGesture(Key.P,ModifierKeys.Control,"Ctrl+P")
                });

            cmdBinding.Command = routedUICmd;
            cmdBinding.CanExecute += new CanExecuteRoutedEventHandler(cmdBinding_CanExecute);
            cmdBinding.Executed += new ExecutedRoutedEventHandler(cmdBinding_Executed);
        }

        private static CommandBinding cmdBinding = new CommandBinding();

        public static CommandBinding CmdBinding
        {
            get { return cmdBinding; }
        }

        static void cmdBinding_Executed(object sender, ExecutedRoutedEventArgs e)
        {
            PrintDocumentToA4Paper();
        }

        internal static void PrintDocumentToA4Paper()
        {
            PaperInfo curPaperInfo = Globals.MainWindow.PageSetting;

            int width, height;
            if (Globals.MainWindow.PageDirection == Orientation.Horizontal)
            {
                width = curPaperInfo.LongSidePixcelCount;
                height = curPaperInfo.ShortSidePixcelCount;
            }
            else
            {
                height = curPaperInfo.LongSidePixcelCount;
                width = curPaperInfo.ShortSidePixcelCount;
            }

            PrintDialog printDialog = new PrintDialog();
            if (printDialog.ShowDialog() != true)
            {
                Globals.MainWindow.Activate();
                //Globals.MainWindow.mainScrollViewer.Focus();//恢复快捷键作用。
                return;
            }

            //取出页面宽、高
            double paperWidth = printDialog.PrintableAreaWidth;
            double paperHeight = printDialog.PrintableAreaHeight;

            Canvas mainCanvas = Globals.MainWindow.mainCanvas;

            bool isHorizontal = false;
            if (mainCanvas.ActualHeight < mainCanvas.ActualWidth) isHorizontal = true;

            Globals.MainWindow.baseBorder.Visibility = Visibility.Hidden;

            if (isHorizontal)
            {
                double longPaperSide = Math.Max(paperWidth, paperHeight);
                double shortPaperSide = Math.Min(paperWidth, paperHeight);

                double xRate = longPaperSide / mainCanvas.ActualWidth;
                double yRate = shortPaperSide / mainCanvas.ActualHeight;

                double rate = Math.Min(xRate, yRate);

                mainCanvas.LayoutTransform = new ScaleTransform(rate, rate);

                printDialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Landscape;
                mainCanvas.Measure(new Size(printDialog.PrintableAreaHeight, printDialog.PrintableAreaWidth));
                mainCanvas.Arrange(new Rect(new Point(0, 0), mainCanvas.DesiredSize));
            }
            else
            {
                //纵向

                double longPaperSide = Math.Max(paperWidth, paperHeight);
                double shortPaperSide = Math.Min(paperWidth, paperHeight);

                double xRate = shortPaperSide / mainCanvas.ActualWidth;
                double yRate = longPaperSide / mainCanvas.ActualHeight;

                double rate = Math.Min(xRate, yRate);

                mainCanvas.LayoutTransform = new ScaleTransform(rate, rate);

                printDialog.PrintTicket.PageOrientation = System.Printing.PageOrientation.Portrait;
                mainCanvas.Measure(new Size(printDialog.PrintableAreaWidth, printDialog.PrintableAreaHeight));
                mainCanvas.Arrange(new Rect(new Point(0, 0), mainCanvas.DesiredSize));
            }

            printDialog.PrintVisual(mainCanvas, "Lunar Mind Printing:" + Globals.MainWindow.GetDefaultFileName());

            mainCanvas.LayoutTransform = null;//恢复

            Globals.MainWindow.baseBorder.Visibility = Visibility.Visible;//为避免打印时闪烁，此前隐藏之。

            MessageBox.Show("　　已将文档发送至打印机！", Globals.AppName,
                MessageBoxButton.OK, MessageBoxImage.Information);
        }

        static void cmdBinding_CanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;//打印
        }
    }
}
